<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../dashboard/mixins/resize'
// import {transform} from 'echarts-stat';
// echarts.registerTransform(transform.histogram);

import * as ecStat from 'echarts-stat';
echarts.registerTransform(ecStat.transform.regression);

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      colors:["#22c119","#ffba0c","#10a0ff","#de94de","#d83006"],
      Options: {
          dataset: []
      }
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    echarts.registerTransform(ecStat.transform.histogram)
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.chartData = {
      //   data: [
      //     [100, 120, 161, 134, 105, 160, 165],
      //     [120, 82, 91, 154, 162, 140, 145],
      //   ],
      //   legend: ['类型a','类型b'],
      //   xdata: ['指标1', '指标2', '指标3', '指标4', '指标5', '指标6', '指标7']
      // }
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      this.chart.setOption({
        dataset: [
            this.chartData,
            {
                transform: {
                    type: 'ecStat:regression', //回归
                    config: {
                        method: 'logarithmic', //对数
                    },
                    // config: { method: 'polynomial', order: 3 }
                    // config: {
                    //   method: 'exponential'
                    // }
                },
            },
        ],

        /*--------------------以下配置，不需要关注------------------*/
        title: {
            text: '趋势线：对数',
            left: 'center',
            axisLabel: {
              textStyle: {
                color: '#333',
              },
            }
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross',
            },
            // formatter: function (params) {
            //     //alert(JSON.stringify(params[0]));
            //     var item = params[0];
            //     var dimensionNames = item.dimensionNames;
            //     var value = item.value;
            //     return (
            //         dimensionNames[1] +
            //         ' : ' +
            //         value[1] +
            //         '<br/>' +
            //         dimensionNames[0] +
            //         ' : ' +
            //         value[0] +
            //         '<br/>' +
            //         dimensionNames[1] +
            //         ' : ' +
            //         value[1]
            //     );
            // },
        },
        xAxis: {
            name: 'X数量',
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                },
            },
            axisLabel: {
              textStyle: {
                color: '#333',
              },
            },
        },
        yAxis: {
            name: 'Y数量',
            splitLine: {
                lineStyle: {
                    type: 'dashed',
                },
            },
            axisLabel: {
              textStyle: {
                color: '#333',
              },
            },
        },
        series: [
            {
                name: 'scatter',
                type: 'scatter',
                datasetIndex: 0,
                // label: {
                //     show: true,
                //     fontSize: 12,
                //     position: 'bottom',
                //     formatter: function (params) {
                //         var item = params;
                //         var value = item.value;
                //         return value[1];
                //     },
                // },
            },
            {
                name: 'line',
                type: 'line',
                smooth: true,
                datasetIndex: 1,
                symbolSize: 0.1,
                symbol: 'circle',
                label: { show: true, fontSize: 12 },
                labelLayout: { dx: -20 },
                encode: { label: 2, tooltip: 1 },
            },
        ],
    })
    }
  }
}
</script>
